package org.raymond.iworks.study.orders.repository;

import org.raymond.iworks.study.orders.entity.TInventories;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface TInventoryRepository extends JpaRepository<TInventories, Long> {

    @Modifying
    @Query(value = "update t_inventories set " +
            " usable_num=usable_num-?2, " +
            " lock_num=lock_num+?2 " +
            " where product_id=?1 and usable_num>0",
            nativeQuery = true)
    int soldInventory(Long productId, Integer num);

    @Modifying
    @Query(value = "update t_inventories set " +
            " usable_num=usable_num+?2, " +
            " lock_num=lock_num-?2 " +
            " where product_id=?1",
            nativeQuery = true)
    int refundInventory(Long productId, Integer num);
}
